import { switchPages } from '@/consts/pages.js'

/**
 * 对象转为字符串参数拼接
 * @param {Object} param 对象参数
 */
function paramsObj2Str(params) {
	// 有参数
	if (Object.keys(params).length != 0) {
		paramUrl += '?'
		for (let k in params) {
			paramUrl += `${k}=${params[k]}&`
		}
		paramUrl = paramUrl.substring(0, paramUrl.length - 1);

		return paramUrl;
	}
	return '';
}

/**
 * 根据秒获取倒计时的时分秒

 * @param {Object} seconds 秒
 */
function getCountdownTime(seconds){
	const h = Math.floor(seconds / 60 / 60);
	const m = Math.floor(seconds / 60 % 60);
	const s = Math.floor(seconds % 60);
	
	return {
		h: h < 10 ? '0' + h : h,
		m: m < 10 ? '0' + m : m,
		s: s < 10 ? '0' + s : s,
	}
	
	
}


/**
 * 刷新当前页面
 */
function refreshCurrentPage() {
	const pages = getCurrentPages();
	const currentPage = pages[pages.length - 1]; //页面
	const params = currentPage.options; // 参数
	let paramUrl = ''; //拼接后的参数
	// 当前页面是tab页面
	if (switchPages.indexOf(currentPage.route) != -1) {
		currentPage.onLoad(params);
		currentPage.onShow();
	} else {
		uni.redirectTo({
			url: currentPage.$page.fullPath
		})
	}
}

// 获取当前页面(组件)实例
function getVM(){
	const pages = getCurrentPages();
	const vm = pages[pages.length - 1].$vm; 
	return vm;
}


export {
	getCountdownTime,
	refreshCurrentPage,
	paramsObj2Str,
	getVM
}